package homeword;

import java.beans.PersistenceDelegate;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Stream;

/**
 * 1.定义一个Person类{name, age, job),初始化Person 对象数组，有3个person对象，并
 * 按照 age从大到小进行排序,提示,使用冒泡排序Homework01.java
 * 2.写出四种访问修饰符和各自的访问权限
 *              同类  同包  子类  不同包
 * public       ✅    ✅     ✅   ✅
 * protected    ✅    ✅     ✅   ✅
 * 默认         ✅    ✅     ❌   ❌
 * private      ✅    ❌     ❌   ❌
 */
public class Test {

    public static void main(String[] args) {
        Person person = new Student("张三", 24, "老师", "xx大学");
        Student student =  (Student)person;
        Person person1 =  (Person)student;
        person1.getAge();
        System.out.println(person1);
//        Person p1 = new Person("张三", 24, "老师");
//        Person p2 = new Person("李四", 20, "老师");
//        Person p3 = new Person("王五", 22, "老师");
//        Person[] persons = new Person[3];
//        persons[0] = p1;
//        persons[1] = p2;
//        persons[2] = p3;
//        int [] arr = new int[3];
//        arr[0] = p1.getAge();
//        arr[1] = p2.getAge();
//        arr[2] = p3.getAge();
//        new Test().bubbleSort(arr);
//        //需要按照顺序排序【i的值就是放的位置】，年龄从小到大排好了已经
//        Person[] newPersons = new Person[3];
//        for (int i = 0; i < arr.length; i++) {
//            for (Person person : persons) {
//                //如果发现年龄相等，就放到排好序的新对象数组里
//                if(arr[i] == person.getAge()){
//                    newPersons[i] = person;
//                }
//            }
//        }
//        System.out.println("====冒泡排序后的数组====");
//        //输出新对象数组
//        for (Person newPerson : newPersons) {
//            System.out.println(newPerson);
//        }

        //我要得根据年龄排序后的person数组对象，再输出;(可以直接用stream流)
//         Arrays.stream(persons).sorted(Comparator.comparingInt(Person::getAge)).forEach(System.out::println);


    }
    //写个冒泡排序
    public void bubbleSort(int [] arr){
        int n = arr.length;
        //一共排序n-1次
        //每次排序里交换 n-1-i(i为排序次数，j为交换次数)
        int temp = 0;
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n-1-i; j++) {
                //从小到大排序，当前面大于后面的时候交换
                if(arr[j] > arr[j+1]){
                    temp = arr[j];
                    arr[j] = arr[j+1];
                    arr[j+1] = temp;
                }
            }
        }
    }

}
class Person{
    private String name;
    private int age;
    private String job;
    public Person(String name, int age, String job) {
        this.name = name;
        this.age = age;
        this.job = job;
    }

    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", job='" + job + '\'' +
                '}';
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getJob() {
        return job;
    }

    public void setJob(String job) {
        this.job = job;
    }
}
class Student extends Person{
    private String school;

    public Student(String name, int age, String job, String school) {
        super(name, age, job);
        this.school = school;
    }

    public String getSchool() {
        return school;
    }
}
